package com.derbysoft.lowcostadapter.web.action.admin.cachemanager.search;

import com.derbysoft.lowcostadapter.commons.model.RateCache;
import com.derbysoft.lowcostadapter.commons.redis.service.RateCacheService;
import com.derbysoft.lowcostadapter.commons.redis.utils.KeyValueHelper;
import com.derbysoft.lowcostadapter.web.action.BaseAdapterAction;
import com.derbysoft.lowcostadapter.web.action.model.RateCondition;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by: jason
 * Date: 2012-09-26
 */
public class RateSearchAction extends BaseAdapterAction {

    private RateCondition rateCondition;

    @Autowired
    private RateCacheService rateCacheRepository;

    @Action("searchRate")
    public void searchRate() {
        try {
            rateCondition.validate();
            List<RateCache> rateCaches = filter(rateCacheRepository.loadAll(rateCondition.makeKey()));
            StringBuilder stringBuilder = new StringBuilder();
            for (RateCache rateCache : rateCaches) {
                stringBuilder.append(rateCache.toFiled()).append(KeyValueHelper.SEPARATOR);
                stringBuilder.append(rateCache.value());
                stringBuilder.append("\n");
            }
            process(StringUtils.isBlank(stringBuilder.toString()) ? NOT_FOUND : stringBuilder.toString());
        } catch (Exception e) {
            populateFailedJsonResult(e);
        }
    }

    private List<RateCache> filter(List<RateCache> rateCaches) {
        List<RateCache> availRates = new ArrayList<RateCache>();
        for (RateCache rateCache : rateCaches) {
            if (check(rateCache)) {
                availRates.add(rateCache);
            }
        }
        return availRates;
    }

    private boolean check(RateCache rateCache) {
        if (StringUtils.isNotBlank(rateCondition.getStart())) {
            if (rateCondition.getStart().compareTo(rateCache.getStartDate()) < 0) {
                return false;
            }
        }
        if (StringUtils.isNotBlank(rateCondition.getRatePlanId())) {
            if (!String.valueOf(rateCache.getMealBasis().getMealBaseID()).equals(rateCondition.getRatePlanId())) {
                return false;
            }
        }

        if (StringUtils.isNotBlank(rateCondition.getRoomTypeId())) {
            if (!String.valueOf(rateCache.getPropertyRoomTypeID()).equals(rateCondition.getRoomTypeId())) {
                return false;
            }
        }
        return true;
    }

    private void process(String value) {
        try {
            populateSucceededJsonResult(value);
        } catch (Exception e) {
            populateFailedJsonResult(e);
        }
    }

    public RateCondition getRateCondition() {
        return rateCondition;
    }

    public void setRateCondition(RateCondition rateCondition) {
        this.rateCondition = rateCondition;
    }
}
